package com.hjx.windowtool.tablemodel;

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Map;

import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;

public class CheckBoxCellRenderer extends DefaultTableCellRenderer  {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3748941264053419659L;
	
	JTableHeader tableHeader;
    final JCheckBox selectBox;

    public CheckBoxCellRenderer(final JTable table) {
    	MapTableModel tableModel = (MapTableModel) table.getModel();
        this.tableHeader = table.getTableHeader();
        selectBox = new JCheckBox(tableModel.getColumnName(0));
        selectBox.setSelected(false);
        tableHeader.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() > 0) {
                    // 获得选中列
                    int selectColumn = tableHeader.columnAtPoint(e.getPoint());
                    if (selectColumn == 0) {
                        boolean value = !selectBox.isSelected();
                        selectBox.setSelected(value);
//                        tableModel.selectAllOrNull(value);
                        tableHeader.repaint();
                        List<Map<String, String>> list = tableModel.getList();
                        ListSelectionModel selectionModel = table.getSelectionModel();
                        for (int i = 0; i < list.size(); i++) {
							Map<String, String> map = list.get(i);
							map.put("checked", value+"");
							if (value) {
								selectionModel.setSelectionInterval(i, i);
							}
						}
                        
                        if (!value){
							selectionModel.clearSelection();
						}else {
							selectionModel.setSelectionInterval(0, list.size()-1);
						}
                        
                        System.out.println(table.getSelectedRowCount());
                    }
                }
            }
        });
    }

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		Component tableCellRendererComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		if (column==0) {
			selectBox.setHorizontalAlignment(SwingConstants.CENTER);// 表头标签剧中
			selectBox.setBorderPainted(true);
			selectBox.setForeground(tableCellRendererComponent.getForeground());
			selectBox.setBackground(tableCellRendererComponent.getBackground());
			selectBox.setFont(tableCellRendererComponent.getFont());
			selectBox.setBorder(tableHeader.getBorder());
//	        selectBox.setBounds(0, 0, 10, tableHeader.getBounds().height);
			return selectBox;
		}
		return tableCellRendererComponent;
	}

}
